import { _decorator, Sprite, SpriteFrame, UITransform } from "cc";
import { app } from "db://app-game/core/app";
import { ViewUtil } from "db://app-game/core/utils/ViewUtil";
import { ecs } from "db://app-game/libs/ecs/ECS";
import { CCComp } from "db://app-game/module/common/CCComp";
import { GameEvent } from "../../../common/config/GameEvent";
import { smc } from "../../../common/ecs/model/SingletonModuleComp";
import { SkillIcon } from "../SkillIcon";

const { ccclass, property } = _decorator;

/**
 * 技能图标组件
 */
@ccclass("SkillIconViewComp")
@ecs.register("SkillIconView", false)
export class SkillIconViewComp extends CCComp {
  skillIcon: SkillIcon = null!;
  protected onLoad() {
    this.skillIcon = this.ent as SkillIcon;
    const { svg, baseAttr } = this.skillIcon.SkillIconModel.data;
    // 1.设置技能图标
    this.node.getComponent(Sprite).spriteFrame = app.res.get(
      svg.base,
      SpriteFrame
    );
    // 2.设置摇杆大小
    this.node.getComponent(UITransform).width = baseAttr.width;
    this.node.getComponent(UITransform).height = baseAttr.height;
    //  3.设置摇杆位置
    const absolutePosition = baseAttr.absolutePosition;
    ViewUtil.AddWidgetComponents(this.node, absolutePosition);
  }

  /** 触发技能 */
  triggerSkill() {
    app.message.dispatchEvent(
      GameEvent.TriggerSkill,
      this.skillIcon.SkillIconModel.data.id
    );
    smc.level.controlRole.triggerSkill(this.skillIcon.SkillIconModel.data.id);
  }

  reset(ent: ecs.Entity): void {
    this.node.destroy();
  }
}
